using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class SnsCommentService
	{
		private const string TAG = "SnsCommentService";
		private const int MAX_RUNNING = 100;
		public const int MAX_RETRY_TIMES = 20;
		private static EventWatcher mWatcher;
		public SnsCommentService()
		{
			SnsCommentService.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.HandlerDoScene));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SYNC, SnsCommentService.mWatcher);
		}
		private void HandlerDoScene(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			NetSceneSyncResult netSceneSyncResult = evtArgs.mObject as NetSceneSyncResult;
			if (netSceneSyncResult == null)
			{
				Log.smethod_1("SnsCommentService", "NetSceneSyncResult == null");
				return;
			}
			if (netSceneSyncResult.syncStatus == SyncStatus.syncEnd)
			{
				SnsCommentService.sendFailedComment();
			}
		}
		public bool doSendComment(SnsInfo snsObj, string strContent, CommentType type, AddContactScene source, CommentArg replyInfo = null, int refCommentID = -1)
		{
			if (snsObj == null)
			{
				return false;
			}
			SnsComment snsComment = new SnsComment();
			snsComment.strContent = strContent;
			snsComment.strNickName = AccountMgr.getCurAccount().strNickName;
			snsComment.strUserName = AccountMgr.getCurAccount().strUsrName;
			snsComment.nType = (uint)type;
			snsComment.nCreateTime = (uint)Util.getNowSeconds();
			snsComment.nSource = (uint)source;
			if (replyInfo != null)
			{
				snsComment.nReplyCommentId = replyInfo.commentID;
				snsComment.strReplyUsername = replyInfo.strUserName;
			}
			if (type != CommentType.MMSNS_COMMENT_LIKE)
			{
				if (type != CommentType.MMSNS_COMMENT_STRANGER_LIKE)
				{
					snsObj.commentList.list.Add(snsComment);
					snsObj.commentList.list = snsObj.commentList.list;
					snsObj.commentList = snsObj.commentList;
					snsObj.nCommentCount++;
					goto IL_100;
				}
			}
			snsObj.likeList.list.Add(snsComment);
			snsObj.likeList.list = snsObj.likeList.list;
			snsObj.likeList = snsObj.likeList;
			snsObj.nLikeFlag = 1;
			snsObj.nLikeCount++;
			IL_100:
			StorageMgr.snsInfo.modify(snsObj);
			SnsCommentNeedSend snsCommentNeedSend = NetSceneSnsComment.creatSnsCommentNeedSend(snsObj, strContent, type, source, replyInfo, refCommentID);
			if (snsCommentNeedSend != null)
			{
				new NetSceneSnsComment().doScene(snsCommentNeedSend);
				SnsCommentNeedSendMap snsCommentNeedSendMap = ConfigMgr.smethod_0<SnsCommentNeedSendMap>();
				if (snsCommentNeedSendMap.dictionary_0.get_Count() <= 0)
				{
					snsCommentNeedSendMap.dictionary_0.Add(snsCommentNeedSend.strClientID, snsCommentNeedSend);
					ConfigMgr.write<SnsCommentNeedSendMap>(snsCommentNeedSendMap);
					return true;
				}
			}
			SnsCommentService.sendFailedComment();
			return true;
		}
		public static void sendFailedComment()
		{
			SnsCommentNeedSendMap snsCommentNeedSendMap = ConfigMgr.smethod_0<SnsCommentNeedSendMap>();
			if (snsCommentNeedSendMap == null || snsCommentNeedSendMap.dictionary_0 == null)
			{
				return;
			}
			if (snsCommentNeedSendMap.dictionary_0.get_Count() <= 0)
			{
				return;
			}
			Dictionary<string, SnsCommentNeedSend>.Enumerator enumerator = snsCommentNeedSendMap.dictionary_0.GetEnumerator();
			while (enumerator.MoveNext())
			{
				KeyValuePair<string, SnsCommentNeedSend> current = enumerator.get_Current();
				SnsCommentNeedSend value = current.get_Value();
				if (value != null)
				{
					if (value.nRetryTimes >= 20)
					{
						snsCommentNeedSendMap.dictionary_0.Remove(value.strClientID);
						if (snsCommentNeedSendMap.dictionary_0.get_Count() <= 0)
						{
							break;
						}
					}
					else
					{
						new NetSceneSnsComment().doScene(value);
					}
				}
			}
			ConfigMgr.write<SnsCommentNeedSendMap>(snsCommentNeedSendMap);
		}
		public static List<SnsCommentNeedSend> getNeedSendCommentFromXml()
		{
			return null;
		}
		public static bool writeCommentToXml(SnsCommentNeedSend comment)
		{
			return true;
		}
		public static bool delCommentFromXml(SnsCommentNeedSend comment)
		{
			return true;
		}
	}
}
